<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { getComboDataApi } from "@/api/stock-operation/stockOrderH5"
import { type IGetComboData } from "@/api/combo/types/combo"
import { type FormInstance, ElMessage } from "element-plus"
import { Search, Refresh, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useUserStore } from "@/store/modules/user"
import vueQr from "vue-qr/src/packages/vue-qr.vue"
import { useCommonStore } from "@/store/modules/common"
defineOptions({
  name: "StockShare"
})
const commonStore = useCommonStore()
const userStore = useUserStore()
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
//#region 增

//#region 查
const tableData = ref<IGetComboData[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  name: "",
  sku: "",
  superSku: "",
  status: "上架"
  // team: userStore.teamName ? userStore.teamName : ""
})
const getTableData = () => {
  loading.value = true
  getComboDataApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    name: searchData.name,
    status: searchData.status,
    report: "否"
  })
    .then((res: any) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const handleRefresh = () => {
  getTableData()
}
//#endregion

/**分享产品 */
import useClipboard from "vue-clipboard3"
const dialogVisibleShare = ref<boolean>(false)
const link = ref<string>("")
const handleShare = (row: any) => {
  dialogVisibleShare.value = true
  link.value = `${commonStore.BASEURL}/company/#/stockOrder?pid=${row.id}&tid=${userStore.usernameID}`
  // link.value = `http://192.168.1.7:3336/#/productDetil?p=${row.id}&u=${userStore.usernameID}`
}
const { toClipboard } = useClipboard()
const copy = async (text: string) => {
  try {
    await toClipboard(text) //实现复制
    ElMessage.success("已复制产品链接！")
  } catch (e) {
    console.error(e)
  }
}

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
/**监听套餐缓存(pinia) */
import { useComboStore } from "@/store/modules/combo"
const comboStore = useComboStore()
comboStore.getComoboFn()
const comboList = ref<any>([])
const comboList1 = ref<any>([])
watch(
  [() => comboStore.comboListStore],
  (newValue) => {
    comboList.value = newValue.flat()
    comboList1.value = newValue.flat().filter((item) => item.state === "true")
  },
  {
    immediate: true
  }
)
/**department END */
// console.log(departTeam.value)

// ** 清除规则报错信息
// const formatterCombo = (row: any, column: any, celValue: any) => {
//   comboList.value.filter((item: any) => {
//     if (item.id == row.package_super) {
//       celValue = item.name
//     }
//   })
//   return celValue
// }
const activeName = ref<any>("shelf")
const handleClick = (tab: any) => {
  paginationData.currentPage = 1

  if (tab.paneName == "shelf") {
    searchData.status = "上架"
  }
  if (tab.paneName == "offShelf") {
    searchData.status = "下架"
  }

  getTableData()
}
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="套餐名">
          <el-input v-model.trim="searchData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新表格">
            <el-button type="primary" :icon="RefreshRight" circle @click="handleRefresh" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
          <el-tab-pane label="已上架产品" name="shelf" />
          <el-tab-pane label="已下架产品" name="offShelf" />
        </el-tabs>
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="image" label="套餐主图" align="center">
            <template #default="scope">
              <img :src="`${commonStore.BASEURL}/image/${scope.row.image?.split(',')[0]}`" alt="" width="80" height="80" />
            </template>
          </el-table-column>
          <el-table-column prop="name" label="套餐名" align="center" />
          <el-table-column prop="code" label="套餐编码" align="center" />
          <el-table-column prop="superName" label="上级套餐名字" align="center" />
          <el-table-column prop="superCode" label="上级套餐编码" align="center" />
          <el-table-column prop="source" label="套餐来源" align="center" />
          <el-table-column prop="operator" label="运营商" align="center">
            <template #default="scope">
              <span v-if="scope.row.operator == 1">移动</span>
              <span v-else-if="scope.row.operator == 2">联通</span>
              <span v-else-if="scope.row.operator == 3">电信</span>
              <span v-else-if="scope.row.operator == 4">广电</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="类型" align="center" />
          <el-table-column prop="state" label="套餐状态" align="center">
            <template #default="scope">
              <span :style="{ color: scope.row.state === '上架' ? '#67c23a' : '#f56c6c' }">{{ scope.row.state }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="isOpen" label="分销商开放" align="center" />
          <el-table-column fixed="right" label="操作" width="280" align="center">
            <template #default="scope">
              <el-button type="success" text bg size="small" @click="handleShare(scope.row)">分享产品</el-button>
              <!-- <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog v-model="dialogVisibleShare" title="分享产品" width="50%">
      <div>
        <span>产品链接：</span>
        <el-link>{{ link }}</el-link>
        <el-link type="primary" style="margin-left: 10px" @click="copy(link)"
          ><el-icon><DocumentCopy /></el-icon>复制</el-link
        >
      </div>
      <div style="margin-top: 30px">
        <span style="vertical-align: top">产品二维码：</span>
        <vue-qr :text="link" :size="200" />
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.ageInput :deep(.el-input) {
  width: 20% !important;
}

.forminput {
  :deep(.el-form-item__label) {
    width: 130px !important;
  }
}

:deep(.el-popper) {
  max-width: 50% !important;
}
:deep(.el-cascader) {
  width: 567px !important;
}
.text-ellipsis {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  display: -webkit-box;
  -webkit-box-orient: vertical;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.el-popper.is-customized {
  /* Set padding to ensure the height is 32px */
  padding: 6px 12px;
  background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

.el-popper.is-customized .el-popper__arrow::before {
  background: linear-gradient(45deg, #b2e68d, #bce689);
  right: 0;
}
</style>
